﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

using VxiUtil;

namespace VxiServer
{
    public class AccountCommunication : TcpCommunication
    {
        protected override byte[] ProccessClientRequest(TcpClient client, byte[] request)
        {
            string accName = request.GetSubByteString(0, 16);
            string accPassword = request.GetSubByteString(16, 16);
            byte data = request[32];

            if (data != null)
            {
                AccountLogin login = new AccountLogin(data);
                if (login.LoginType == AccountLoginType.Login)
                {
                    ClientUser user = new ClientUser(client, 1000);
                    ClientManagement.AddClient(client.Client.GetIPAddress(), user);
                    byte[] returnValue = new byte[] { Convert.ToByte(AccountLoginReturnType.Success) };
                    return returnValue.AppendArray(BitConverter.GetBytes(user.AccountID));
                }
                else if (login.LoginType == AccountLoginType.Create)
                {
                    return new byte[] { Convert.ToByte(AccountLoginReturnType.CreateSuccess) };
                }                
            }
            return null;
        }

        protected override int Port
        {
            get { return 54231; }
        }
    }
}
